/*

Homepage Slider v1.0

Copyright (C) ThemePrince.com - All rights reserved!

*/



jQuery.fn.hpSlider = function() {	
	var args = arguments[0] || {};    
	
	var fade_speed = args.speed;	//duration of fadeout and fadein
	if(!fade_speed){fade_speed = 1200;}
		
	var delay_before_next = 500;	//delay before next slide
	
	var slides = args.slides;		//slide images and their parameters in an array
	var t;
	
	//START SLIDING
	if(args.start === true){
		
	}
	
	//PARAMETERS	
	if(slides)
	{
		var num_of_slides = slides.length;	//number of slide images				
		var this_div = jQuery(this);
		
		//ADD IMAGES
		for (j=0; j < num_of_slides; j++){					
			this_div.append('<div id="hp_slider-'+j+'" class="hp_slider-img"></div>');
					
			if(j == 0){
				jQuery('#hp_slider-'+j).css({'background-image':'url('+ slides[j]['image'] +')'});
			}else{
				jQuery('#hp_slider-'+j).css({'opacity':'0','background-image':'url('+ slides[j]['image'] +')'});
			}
		}
	
				
		
		//ROTATING LOOP
		var i = 0; 	//current index
		var change = function(){	
			var j = i + 1;
			
						
			if(num_of_slides == j){
				j = 0;
			}
		
					
			//FADE OUT CURRENT SLIDE 

				//stop animations first
				jQuery('.all_particles').clearQueue();				
				jQuery('.all_particles').fadeOut(500,function(){
					jQuery('.all_particles').stop();
					jQuery('.all_particles').remove();
				});
				
				//IF IE8 or lower, delay flare fadeout
				var delay_flare_fadeout = 0;
				if(jQuery.browser.msie){
					if(jQuery.browser.version < 9){
						delay_flare_fadeout = 500;
					}
				}
				
				jQuery('#lens_flare').delay(delay_flare_fadeout).fadeOut(500,function(){
					jQuery(this).css('display','block');
					jQuery('#lens_flare').Flares({
						'stop':true
					})
				});
				
				
			
			jQuery('#hp_slider-'+i).animate({'opacity':'0'},{ 
				duration: fade_speed,
				complete: function(){
					
					
					//check parameters, apply to current slide					
					if(slides[j]['particles'] === true){
						jQuery('#particles').Particles({
							'delay':delay_before_next
						});						
					}
					
					if(slides[j]['resize'] === true){
						jQuery('#hp_slider-'+j).addClass('autoresize');
					}
					
					if(slides[j]['lens_flare'] === true){
						jQuery('#lens_flare').Flares({
							'color': slides[j]['color']
						});
					}
					
					//fade in next slide				
					jQuery('#hp_slider-'+j).delay(delay_before_next).animate({'opacity':'1'},{ 
						duration: fade_speed,
						complete: function(){	
														
							var pause = slides[j]['pause'];
							if(!pause){pause = 5000;}
							
							
							//change current index
							i = j;
							
							//start over
							setTimeout(change,pause);
						}
					});
				}
			});		
		};
		
		
		//START
		if(num_of_slides > 1){
			
			var pause = slides[0]['pause'];
			if(!pause){pause = 5000;}

			//check parameters, apply to current slide					
			if(slides[0]['particles'] === true){
				jQuery('#particles').Particles({
					'delay':delay_before_next
				});						
			}		
			
			if(slides[0]['resize'] === true){
				jQuery('#hp_slider-0').addClass('autoresize');
			}
			
			if(slides[0]['lens_flare'] === true){
				jQuery('#lens_flare').Flares({
					'delay':delay_before_next,
					'color': slides[0]['color']
				});
			}
			
			//start rotating only when all elements are loaded
			jQuery(window).load(
			function(){				
				pause = pause + 1000;
				var t=setTimeout(change,pause);				
			});
			
		}else if(num_of_slides == 1){
			//ONE IMAGE ONLY
			//check parameters, apply to current slide					
			if(slides[0]['particles'] === true){
				jQuery('#particles').Particles({
					'delay':delay_before_next
				});						
			}		
			
			if(slides[0]['resize'] === true){
				jQuery('#hp_slider-0').addClass('autoresize');
			}
			
			if(slides[0]['lens_flare'] === true){
				jQuery('#lens_flare').Flares({
					'delay':delay_before_next,
					'color': slides[0]['color']
				});
			}
		}
		
		
		
	}
};